//
//  HMEmoticonToolBar.swift
//  表情键盘
//
//  Created by Apple on 16/7/28.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

enum EmoticonToolBarType: Int {
    case Recent = 0     // 最近
    case Default = 1     // 默认
    case Emoji = 2     // emoji
    case LXH = 3     // 浪小花
}

/// 1.按钮选中的代理
protocol HMEmoticonToolBarDelegate: NSObjectProtocol {
    // 2.toolBar选中了一个按钮
    // 默认是必须实现的方法
    // optional: 可选的,可以实现可以不实现
    func emoticonToolBar(didSelectedType type: EmoticonToolBarType)
}

/// 下面4个按钮的toolBar
class HMEmoticonToolBar: UIView {
    
    // 3.定义代理属性
    weak var delegate: HMEmoticonToolBarDelegate?
    
    /// 4个按钮数组
    private var buttons: [UIButton] = [UIButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    /// 设置界面
    private func setupUI() {
        let names = ["最近", "默认", "emoji", "浪小花"]
        
        for (index, name) in names.enumerate() {
            // 创建按钮
            let button = UIButton()
            
            button.setTitle(name, forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_normal"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Disabled)
            button.tag = index
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            self.addSubview(button)
            buttons.append(button)
            
            if index == 0 {
                // 默认选中这个
                buttonClick(button)
            }
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 父控件的frame就确定了
        // 给4个按钮设置frame,一个view要么使用约束,要么使用frame,但是一个View不能即使用约束又使用frame
        let buttonWidth = self.frame.width / CGFloat(buttons.count)
        let buttonHeight = self.frame.height
        
        for (index, button) in buttons.enumerate() {
            button.frame = CGRectMake(CGFloat(index) * buttonWidth, 0, buttonWidth, buttonHeight)
        }
    }
    
    // MARK: - 按钮点击事件
    @objc private func buttonClick(button: UIButton) {
        // 选中一个按钮
        switchSelectedButton(button)
        
        // 让collectionView滚动
        let tyep = EmoticonToolBarType(rawValue: button.tag)!
        delegate?.emoticonToolBar(didSelectedType: tyep)
    }
    
    /// 选中一个按钮,通过section
    func switchSelectedButton(section: Int) {
        // 根据传入的section来找到对应的按钮
        let button = buttons[section]
        
        switchSelectedButton(button)
    }
    
    /// 选中一个按钮
    private func switchSelectedButton(button: UIButton) {
//        print("我选中了: \(button.tag)")
        
        // 1.之前按钮可用
        selectedButton?.enabled = true
        
        // 2.将当前按钮不可用
        button.enabled = false
        
        // 3.将选中的按钮记录起来
        selectedButton = button
    }
    
    /// 记录当前选中按钮
    private var selectedButton: UIButton?
}
